package com.moonLight.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.moonLight.weblog.common.domain.dos.UserDo;

import java.time.LocalDateTime;

/**
 * 操作数据的时候 是Do类型
 */

public interface UserMapper extends BaseMapper<UserDo> {

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    default UserDo findByUsername(String username) {
        LambdaQueryWrapper<UserDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDo::getUsername, username);
        return selectOne(wrapper);
    }
    /**
     * 根据邮箱查询用户
     * @param email
     * @return
     */
    default UserDo findByEmail(String email) {
        LambdaQueryWrapper<UserDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDo::getEmail, email);
        return selectOne(wrapper);
    }

    /**
     * 根据用户名修改密码
     * @param username  用户名
     * @param password  新密码
     * @return int
     */

    default int updatePasswordByUsername(String username, String password) {
        LambdaUpdateWrapper<UserDo> wrapper = new LambdaUpdateWrapper<>();
        //设置查询条件
        wrapper.eq(UserDo::getUsername, username);
        //设置更新信息
        wrapper.set(UserDo::getPassword, password);
        wrapper.set(UserDo::getUpdateTime, LocalDateTime.now());

        return update(null, wrapper);
    }

}
